package cn.com.cym.gamezao.web.authorize;

import cn.com.cym.gamezao.annotation.AuthToken;
import cn.com.cym.gamezao.base.SearchPage;
import cn.com.cym.gamezao.entity.LocalFile;
import cn.com.cym.gamezao.entity.authorize.UserInfo;
import cn.com.cym.gamezao.service.LocalFileService;
import cn.com.cym.gamezao.service.authorize.UserInfoService;
import cn.com.cym.gamezao.util.CommonUtils;
import cn.com.cym.gamezao.web.LocalFileController;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Api(tags = "a-用户基本信息")
@RestController
@RequestMapping(value = "userInfo")
public class UserInfoController {

    final UserInfoService thisService;

    final LocalFileService localFileService;

    public  UserInfoController(UserInfoService thisService,LocalFileService localFileService){
        this.thisService=thisService;
        this.localFileService=localFileService;
    }

    @ApiImplicitParam(name = "userInfo", value = "userInfo", required = true,dataType = "UserInfo")
    @ApiOperation(value = "保存用户信息")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @PostMapping()
    public ResponseEntity<?> save(@RequestBody UserInfo userInfo){
        final MultipartFile file = userInfo.getFileStr();
        final LocalFile localFile = LocalFileController.saveFile(file, localFileService);
        userInfo.setHeadPic(localFile.getUuidName());
        return ResponseEntity.ok(thisService.save(userInfo));
    }

    @ApiImplicitParam(name = "searchPage", value = "searchPage", required = true,dataType = "SearchPage")
    @ApiOperation(value = "分页显示page！")
    @ApiOperationSupport(order = 4, author = "mr.chen")
    @PostMapping("/page")
    @AuthToken
    public ResponseEntity<?> page(@RequestBody SearchPage searchPage){
        Page<?> byPageable = this.thisService.findByPageable(searchPage);
        return ResponseEntity.ok(byPageable);
    }


    @ApiOperation(value = "获取当前用户信息")
    @ApiOperationSupport(order = 6, author = "mr.chen")
    @AuthToken
    @GetMapping("")
    public ResponseEntity<?> getUserInfo(){
        return ResponseEntity.ok(this.thisService.getUserInfo());
    }
}
